
int [] m_iPlayerY = {130, 160, 190, 220};
int [] m_iScoreY = {130, 160, 190, 220};
int m_iPlayerX = 50;
int m_iScoreX = SCREEN_W - 50;

RecordStore recHighScore1;
String strPlayer[] = new String[]{"NGOC THACH", "NGOC THACH", "NGOC THACH", "NGOC THACH"};
int iScore[] = new int[4];

void UpdateHighScore(int mode)
{
	if (mode == UpdateType.UPDATE)
	{
		if(IsKeyPressed(DK_SOFT_RIGHT)&&!isIGMenu)
			SwitchState(GameState.MainMenu);
		if(IsKeyPressed(DK_SOFT_RIGHT)&&isIGMenu)
			SwitchState(GameState.InGameMenu);	
	} 

	if (mode == UpdateType.PAINT)
	{
		// s_g.drawImage(m_imgMainMenuBackground, 0, 0, Graphics.TOP|Graphics.LEFT);
		// s_g.setColor(0x202020);
		// s_g.fillRect(0, 283, 240, 19);
		// s_g.setColor(0x000000);
		// s_g.fillRect(0, 302, 240, 18);
		// s_g.setColor(0xE2E2E2);
		//s_g.drawString("High Scores", SCREEN_W/2 + 1, 61, Graphics.TOP|Graphics.HCENTER);
		if (m_imgOptionBG != null)
		{
			s_g.drawImage(m_imgOptionBG, 0, 0, Graphics.TOP|Graphics.LEFT);
		}	
		fonts[1].drawString("HIGHT SCORE",SCREEN_W>>1,30,Graphics.HCENTER,s_g);
		fonts[1].drawString("PLAYER",m_iPlayerX,m_iPlayerY[0] - 40,Graphics.HCENTER,s_g);
		fonts[1].drawString("SCORE",m_iScoreX,m_iPlayerY[0] - 40,Graphics.HCENTER,s_g);
		//s_g.setColor(0x1B5F89);
		// s_g.drawString("High Scores", SCREEN_W/2, 60, Graphics.TOP|Graphics.HCENTER);
		// s_g.drawString("Player", m_iPlayerX, m_iPlayerY[0] - 40, Graphics.TOP|Graphics.HCENTER);
		// s_g.drawString("Time", m_iScoreX, m_iScoreY[0] - 40, Graphics.TOP|Graphics.HCENTER);
		for(int i = 0; i < 4; i++)
		{
			// s_g.setColor(0);
			// s_g.drawString(strPlayer[i], m_iPlayerX, m_iPlayerY[i], Graphics.TOP|Graphics.HCENTER);
			// s_g.drawString(Integer.toString(iScore[i]), m_iScoreX, m_iScoreY[i], Graphics.TOP|Graphics.HCENTER);
			fonts[1].drawString(strPlayer[i],m_iPlayerX+4,m_iPlayerY[i],Graphics.HCENTER,s_g);
			fonts[1].drawString(Integer.toString(iScore[i]),m_iScoreX,m_iScoreY[i],Graphics.HCENTER,s_g);
		}
		// s_g.setColor(0xffffff);
		// s_g.drawString("Back", SCREEN_W - 12, SCREEN_H - 16, Graphics.BASELINE|Graphics.RIGHT);
	}

	if (mode == UpdateType.CTOR)
	{   
	    try{
	         m_imgOptionBG = Image.createImage("/bg_pink_02.png");	
	       }
	catch(Exception e){}
		System.out.println("open recordstore");
		openRecStore();
		System.out.println("read recordstore");
		readRecords();
		System.out.println("close recordstore");
		//writeRecord("player",999);
		closeRecStore();
	}
	if(mode == UpdateType.DTOR)
	{
	m_imgOptionBG=null;
	}
}

public void openRecStore()
{
    try
    {
		recHighScore1 = RecordStore.openRecordStore("HighScore1", true );
    }
    catch (Exception e)
    {
      db("open " + e.toString());
    }
}    
  
public void closeRecStore()
{
    try
    {
      recHighScore1.closeRecordStore();
    }
    catch (Exception e)
    {
      db("clode " + e.toString());
    }
}

public void deleteRecStore()
{
    if (RecordStore.listRecordStores() != null)
    {
		try
		{
			RecordStore.deleteRecordStore("HighScore");
		}
		catch (Exception e)
		{
			db(e.toString());
		}
    }      
}

public void writeRecord(String strPlayer,int iScore)
{
    ByteArrayOutputStream strmBytes = new ByteArrayOutputStream();
	DataOutputStream strmDataType = new DataOutputStream(strmBytes);   
    byte[] record;
    try
    {
		//write data
		strmDataType.writeUTF(strPlayer);
        strmDataType.writeInt(iScore);
		// Clear any buffered data
        strmDataType.flush();

        // Get stream data into byte array and write record      
        record = strmBytes.toByteArray();
        recHighScore1.addRecord(record, 0, record.length);      

        // Toss any data in the internal array so writes 
        // starts at beginning (of the internal array)
        strmBytes.reset();
		strmBytes.close();
		strmDataType.close();
    }
    catch (Exception e)
    {
      db("write" + e.toString());
    }
}

public void readRecords()
{
    try
    {
		// Careful: Make sure this is big enough!
		// Better yet, test and reallocate if necessary
		byte[] recData = new byte[50];

		// Read from the specified byte array
		ByteArrayInputStream strmBytes = new ByteArrayInputStream(recData);

		// Read Java data types from the above byte array
		DataInputStream strmDataType = new DataInputStream(strmBytes);

		if (recHighScore1.getNumRecords() > 0)
		{
		ComparatorString comp = new ComparatorString();
        RecordEnumeration re = recHighScore1.enumerateRecords(null, comp, false);
		int i = 0;
        while (re.hasNextElement())
        {
			// Get data into the byte array          
			recHighScore1.getRecord(re.nextRecordId(), recData, 0);
			strPlayer[i] = strmDataType.readUTF();
			iScore[i] = strmDataType.readInt();                    
			System.out.println("---------read record-----------" + i);        
			i++;
          // Reset so read starts at beginning of array 
			strmBytes.reset();
        }    
        // Free enumerator
        re.destroy();  
		}
		strmBytes.close();
		strmDataType.close();
    }
    catch (Exception e)
    {
      db("read" + e.toString());
    }
}
class ComparatorString implements RecordComparator
{
  private byte[] recData = new byte[10];

  // Read from a specified byte array    
  private ByteArrayInputStream strmBytes = null;
  
  // Read Java data types from the above byte array
  private DataInputStream strmDataType = null;
  
  public void compareStringClose()
  {
    try
    {
      if (strmBytes != null)
        strmBytes.close();
      if (strmDataType != null)
        strmDataType.close();
    }
    catch (Exception e)
    {}
  }
  
  public int compare(byte[] rec1, byte[] rec2)
  {
    String str1, str2;
    
    try
    {
      // If either record is larger than our buffer, reallocate
      int maxsize = Math.max(rec1.length, rec2.length);
      if (maxsize > recData.length)
        recData = new byte[maxsize];

      // Read record #1
      // Only need one read because the string to
      // sort on is the first "field" in the record
      strmBytes = new ByteArrayInputStream(rec1);
      strmDataType = new DataInputStream(strmBytes);
      str1 = strmDataType.readUTF();

      // Read record #2      
      strmBytes = new ByteArrayInputStream(rec2);
      strmDataType = new DataInputStream(strmBytes);
      str2 = strmDataType.readUTF();  
      
      // Compare record #1 and #2      
      int result = str1.compareTo(str2);
      if (result == 0)
        return RecordComparator.EQUIVALENT;
      else if (result < 0)
        return RecordComparator.PRECEDES;
      else
        return RecordComparator.FOLLOWS;          
      
    }      
    catch (Exception e)
    { 
      return RecordComparator.EQUIVALENT;
    } 
  }
}  
void db(String str)
{
	System.err.println("Msg: " + str);
}